﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;
using System.Windows.Input;

namespace FruitManagerApp
{
    public class UserViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private UserModel gUserModel;

        public UserViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gUserModel = new UserModel();

            UpdateList();
            UpdateUI();
        }

        public void UpdateUI()
        {

        }

        public void UpdateList()
        {
            Province.Clear();
            HttpResponse response = API.SelectProvince();
            if (response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }
        }

        public ObservableCollection<CUser> DataList
        {
            get
            {
                return gUserModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CProvince> province = new ObservableCollection<CProvince>();
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        private int cityUID;
        public int CityUID
        {
            get
            {
                return cityUID;
            }

            set
            {
                cityUID = value;
                RaisePropertyChanged("CityUID");
            }
        }

        private ObservableCollection<CCity> city = new ObservableCollection<CCity>();
        public ObservableCollection<CCity> City
        {
            get
            {
                return city;
            }

            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        private int officeUID;
        public int OfficeUID
        {
            get
            {
                return officeUID;
            }

            set
            {
                officeUID = value;
                RaisePropertyChanged("OfficeUID");
            }
        }

        private ObservableCollection<COffice> office = new ObservableCollection<COffice>();
        public ObservableCollection<COffice> Office
        {
            get
            {
                return office;
            }

            set
            {
                office = value;
                RaisePropertyChanged("Office");
            }
        }

        #region Command
        protected override void AddCommand(object obj)
        {
            UserAdd window = new UserAdd();
            UserAddViewModel viewModel = new UserAddViewModel(window, this);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected override void DeleteCommand(object obj)
        {
            List<String> list = new List<String>();
            foreach (var item in DataList)
            {
                if (item.IsChoose)
                {
                    list.Add(item.UID);
                }
            }

            if (list.Count != 0)
            {
                API.DeleteUser(list);
                UpdateList();
            }
        }

        protected override void CheckedCommand(object obj)
        {
            if(IsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void ItemEditClick(object obj)
        {
            //UserEdit window = new UserEdit();
            //UserEditViewModel viewModel = new UserEditViewModel(window, this, (CUser)obj);
            //window.DataContext = viewModel;
            //window.ShowDialog();
        }

        protected DelegateCommand<object> _provinceChanged;
        public DelegateCommand<object> _ProvinceChanged
        {
            get
            {
                return _provinceChanged ?? (_provinceChanged = new DelegateCommand<object>(ProvinceChanged));
            }
        }

        protected virtual void ProvinceChanged(object obj)
        {
            City.Clear();
            Office.Clear();
            DataList.Clear();
            HttpResponse response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                if (response.D != "")
                {
                    List<CCity> list = JSON.parse<List<CCity>>(response.D);
                    foreach (var item in list)
                    {
                        City.Add(item);
                    }
                }
            }
        }

        protected DelegateCommand<object> _cityChanged;
        public DelegateCommand<object> _CityChanged
        {
            get
            {
                return _cityChanged ?? (_cityChanged = new DelegateCommand<object>(CityChanged));
            }
        }

        protected virtual void CityChanged(object obj)
        {
            Office.Clear();
            DataList.Clear();
            HttpResponse response = API.SelectOffice(ProvinceUID, CityUID);
            if (response.S == HttpResponse.Success)
            {
                if (response.D != "")
                {
                    List<COffice> list = JSON.parse<List<COffice>>(response.D);
                    foreach (var item in list)
                    {
                        Office.Add(item);
                    }
                }
            }
        }

        protected DelegateCommand<object> _officeChanged;
        public DelegateCommand<object> _OfficeChanged
        {
            get
            {
                return _officeChanged ?? (_officeChanged = new DelegateCommand<object>(OfficeChanged));
            }
        }

        protected virtual void OfficeChanged(object obj)
        {
            DataList.Clear();
            HttpResponse response = API.SelectUser(OfficeUID);
            if (response.S == HttpResponse.Success)
            {
                if (response.D != "")
                {
                    List<CUser> list = JSON.parse<List<CUser>>(response.D);
                    foreach (var item in list)
                    {
                        DataList.Add(item);
                    }
                }
            }
        }
        #endregion
    }
}
